/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.operation.controller;

import cn.icanci.basic.commonutils.R;
import cn.icanci.operation.entity.AdminInfo;
import cn.icanci.operation.service.AdminInfoService;
import cn.icanci.operation.utils.TradeLogService;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import com.alibaba.nacos.client.naming.utils.StringUtils;

/**
 * @author icanci
 * @date Created in 2021/04/10 22:15
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/operationAnalysis")
public class OperationAnalysisController {
    @Resource
    private TradeLogService  tradeLogService;
    @Resource
    private AdminInfoService adminInfoService;

    @PostMapping("/login")
    public R adminLogin(@RequestBody AdminInfo adminInfo) {
        try {
            tradeLogService.setLogInfo("OPERATION_ANALYSIS", "LOGIN", "");
            tradeLogService.info(log, "admin login ,adminLoginVO:{}", adminInfo);
            AdminInfo temp = adminInfoService.queryOneRow(adminInfo);
            if (null == temp) {
                return R.error().message("账户密码错误");
            }
            return R.ok().data("token", temp.getUsername());
        } finally {
            tradeLogService.removeAll();
        }
    }

    @GetMapping("/info/{username}")
    public R info(@PathVariable("username") String username) {
        try {
            if (StringUtils.isEmpty(username)) {
                return R.error().message("用户已经退出");
            }
            tradeLogService.setLogInfo("OPERATION_ANALYSIS", "INFO", "");
            tradeLogService.info(log, "get info ,username:{}", username);
            AdminInfo adminInfo = adminInfoService.queryOneRowByUsername(username);
            if (null == adminInfo) {
                return R.error().message("用户不存在");
            }
            return R.ok().data("roles", adminInfo.getRoles()).data("name", adminInfo.getUsername()).data("avatar", adminInfo.getAvatar());
        } finally {
            tradeLogService.removeAll();
        }
    }

    @PostMapping("/logout")
    public R logout() {
        try {
            tradeLogService.setLogInfo("OPERATION_ANALYSIS", "LOGOUT", "");
            tradeLogService.info(log, "logout");
            return R.ok().message("退出成功");
        } finally {
            tradeLogService.removeAll();
        }
    }
}
